﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;
using System.Web.UI.HtmlControls;


namespace MyVocationSpace
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        PagedDataSource source;
        int pos;
        protected void Page_Load(object sender, EventArgs e)
        {
            BAL.conn.connectionString = ConfigurationManager.ConnectionStrings["connectionstring"].ConnectionString;

            if (Session["UserID"] != null)
            {
                //if (Request.QueryString["topicId"] != null && Request.QueryString["IsMentor"] !="")
                //{
                        int IsMentor=1;                   
                        //Request.QueryString["IsMentor"] = "1";
                        int TopicId = Convert.ToInt32(Request.QueryString["topicId"]);
                        IsMentor = Convert.ToInt32(Request.QueryString["IsMentor"]);
                         ViewState["TOPICID"] = TopicId;
                        ViewState["ISMENTOR"] = IsMentor;

                        getcomment();                   
                //}
                //else
                //{
                    if (!IsPostBack)
                    {
                        ViewState["vs"] = 0;
                        GetData();
                        if (DataList1.Items.Count == 0)
                        {
                            Allreplies.Visible = false;
                            HRuler.Visible = false;
                        }
                        else
                        {
                            Allreplies.Visible = true;
                            HRuler.Visible = true;
                        }

                    }
                   

                    
                     pos = (int)this.ViewState["vs"];
              //}
            }
            else
            {
                Response.Redirect("Home.aspx");
            }
        }
        public void getcomment()
        {
            int tid = Convert.ToInt32(ViewState["TOPICID"]);
            int IsMentor = Convert.ToInt32(ViewState["ISMENTOR"]);
            DataSet ds = BAL.Vocation.SelectCommentDetails(tid,IsMentor);
            source = new PagedDataSource();
            source.DataSource = ds.Tables[0].DefaultView;
            source.PageSize = 4;
            source.AllowPaging = true;
            source.CurrentPageIndex = pos;
            //btnfirst.Enabled = !source.IsFirstPage;
            //btnprevious.Enabled = !source.IsFirstPage;
            //btnlast.Enabled = !source.IsLastPage;
            //btnnext.Enabled = !source.IsLastPage;
          
            DataList1.DataSource = source;
            DataList1.DataBind();                
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            getcomment();
        }

        public void GetData()
        {
            int TopicId = Convert.ToInt32(Request.QueryString["topicId"]);
            int IsMentor = Convert.ToInt32(Request.QueryString["IsMentor"]);
            string[] details = BAL.Vocation.SelectTopicDetails(TopicId);
            
            //lblImage.Text = details[0];
            string img = details[1];
            if (img != "")
            {
                Img.Src = "UsersPhoto/" + img;
            }

            else
            {
                Img.Src = "UsersPhoto/icon_no_photo_60x60.png";
            }

            hdTopicID.Value = details[0];
            hdIsMentor.Value =Convert.ToString(IsMentor);
            lblUsername.Text = details[2];
            lblUserVName.Text = details[3];
            lbltopicName.Text = details[4];
            lblPostedDate.Text = details[5];
            lblVocationTName.Text = details[6];
            lbldescription.Text = details[7];
        }

        protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                HtmlImage htmlImg = (HtmlImage)e.Item.FindControl("imgPerson");

                int IdUser = Convert.ToInt32(DataList1.DataKeys[e.Item.ItemIndex]);
                string img = htmlImg.Src;

                if (htmlImg.Src == "")
                {
                    htmlImg.Src = "UsersPhoto/icon_no_photo_60x60.png";
                }
                else
                {
                    htmlImg.Src = "UsersPhoto/" + img;
                }
            }
        }

        //protected void btnfirst_Click(object sender, EventArgs e)
        //{
        //    pos = 0;
        //    getcomment();
        //    ////btnfirst.Enabled = true;
        //    //btnlast.Enabled = true;
        //    //btnnext.Enabled = true;
        //    //btnprevious.Enabled = true;
        //}

        //protected void btnprevious_Click(object sender, EventArgs e)
        //{
        //    pos = (int)this.ViewState["vs"];
        //    pos -= 1;
        //    this.ViewState["vs"] = pos;
        //    getcomment();
        //    //btnfirst.Enabled = true;
        //    //btnlast.Enabled = true;
        //    //btnnext.Enabled = true;
        //    //btnprevious.Enabled = true;
        //}

        //protected void btnnext_Click(object sender, EventArgs e)
        //{
        //    pos = (int)this.ViewState["vs"];
        //    pos += 1;
        //    this.ViewState["vs"] = pos;
        //    getcomment();
        //    //btnfirst.Enabled = true;
        //    //btnlast.Enabled = true;      
        //    //btnprevious.Enabled = true;
        //}

        //protected void btnlast_Click(object sender, EventArgs e)
        //{
        //    pos = source.PageCount - 1;
        //    getcomment();           
            
        //    //btnnext.Enabled = false;
        //    //btnprevious.Enabled = false;
        //}

      
    }
}